package com.handydata.antifraud.ws.dap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handydata.antifraud.annotation.Comment;
import com.handydata.antifraud.common.constant.DataSourceCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.AntiFraudBlackListReqTO;
import com.handydata.antifraud.to.dap.AntiFraudBlackListResTO;
import com.handydata.antifraud.util.DateUtil;
import com.handydata.antifraud.ws.WsInvocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 逾期催收黑名单详情
 */
@Slf4j
@Service("antiFraudBlackListWsService")
@Comment("小视催收黑名单")
public class AntiFraudBlackListWsServiceImpl extends DataPlatformWsService<AntiFraudBlackListReqTO, AntiFraudBlackListResTO>
	implements WsInvocationService<AntiFraudBlackListReqTO, AntiFraudBlackListResTO> {
	
	@Override
	public AntiFraudBlackListResTO query(AntiFraudBlackListReqTO req) throws WsInvokeException {
		log.info("小视催收黑名单 {}", req);
		AntiFraudBlackListResTO resTO = new AntiFraudBlackListResTO();
		if (StringUtils.isEmpty(req.getName())) {
			log.error("小视催收黑名单参数校验 {}", "name为空");
			resTO.setResCode("1111");
			resTO.setResMsg("小视催收黑名单接口name参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getIdCard())) {
			log.error("小视催收黑名单参数校验 {}", "idCard为空");
			resTO.setResCode("1111");
			resTO.setResMsg("小视催收黑名单接口idCard参数为空");
			return resTO;
		}
		if (StringUtils.isEmpty(req.getPhoneNum())) {
			log.error("小视催收黑名单参数校验 {}", "phoneNum为空");
			resTO.setResCode("1111");
			resTO.setResMsg("小视催收黑名单接口phoneNum参数为空");
			return resTO;
		}
		this.dataType = DataSourceCode.ANTI_FRAUD_BLACK.getCode();
		resTO = executeQuery(req);
		return resTO;
	}

	@Override
	protected String createQueryXml(AntiFraudBlackListReqTO req) {
		StringBuilder xml = new StringBuilder();
		xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
		xml.append("<subatm>");
		xml.append("<application>GwBiz.Req</application>");
		xml.append("<version>1.0.0</version>");
		xml.append("<sendTime>").append(DateUtil.now14()).append("</sendTime>");
		xml.append("<transCode>101012</transCode>");
		xml.append("<channelId>").append(encryInfo.getChannelId()).append("</channelId>");
		xml.append("<channelOrderId>").append(DateUtil.now17()).append("</channelOrderId>");
		xml.append("<name>").append(req.getName()).append("</name>");
		xml.append("<idCard>").append(req.getIdCard()).append("</idCard>");
		xml.append("<mobile>").append(req.getPhoneNum()).append("</mobile>");
		xml.append("</subatm>");
		req.setQueryXml(xml.toString());
		return xml.toString();
	}

	@Override
	protected AntiFraudBlackListResTO parseResultAndSave(String resultXml,	String orderId, Date createDate) {
		log.info("调用逾期催收黑名单详情接口返回报文：{}", resultXml);
		AntiFraudBlackListResTO resTO = new AntiFraudBlackListResTO();
		try {
			// 解析xml，保存本地DB
			JSONObject jasonObject = JSONObject.parseObject(resultXml);
			if (null != jasonObject) {

				if ("0000".equals(jasonObject.get("resCode"))) {
					// 统计逾期催收黑名单详情中被催收的次数
					JSONArray jsonArray = (JSONArray) jasonObject.get("resData");
					resTO.setCountOfDataAntiFraudBlackList(jsonArray.size());
					resTO.setDataAntiFraudBlackList(jasonObject.getString("resData"));
				} 
				resTO.setResCodeOfDataAntiFraudBlackList((String)jasonObject.get("resCode"));
				resTO.setResMsgOfDataAntiFraudBlackList((String)jasonObject.get("resMsg"));
				resTO.setResCode((String)jasonObject.get("resCode"));
				resTO.setResMsg((String)jasonObject.get("resMsg"));
				//resTO.setReqId(dataCallLogsReqBO.getId());
			} else {
				resTO.setResCode("1111");
				resTO.setResMsg("逾期催收黑名单详情接口返回数据异常");
			}
		} catch (Exception e) {
			log.info("调用逾期催收黑名单详情接口异常：{}", e.getMessage());
			resTO.setResCode("1111");
			resTO.setResMsg("调用逾期催收黑名单详情接口异常");
		}
		log.info("调用逾期催收黑名单详情接口返回结果：{}", JSONObject.toJSONString(resTO));
		return resTO;
	}

	@SuppressWarnings("unchecked")
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, AntiFraudBlackListReqTO.class);
	}
	
	public static void main(String[] args) {
		String aa = "{\"resCode\":\"0000\",\"resMsg\":\"查询成功\",\"resData\":[{\"Date\":\"2017/07/2318:17:42\",\"money\":\"2\",\"platForm\":\"49\"},{\"Date\":\"2017/10/2219:59:01\",\"money\":3,\"platForm\":\"52\"}]}";
		JSONObject jasonObject = JSONObject.parseObject(aa);
		System.out.println("==============" + jasonObject);
		JSONArray jsonArray = (JSONArray) jasonObject.get("resData");
		System.out.println(jsonArray.size() + "==============" + jsonArray);
	}


}

